package com.cloudptt.api.product.func.call.group.fsm.video;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.func.call.group.fsm.call.GroupCallAgent;
import com.cloudptt.api.product.func.call.group.fsm.floor.ISubState;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;

public class VideoPendingReleaseState implements ISubState,IRollback
{
	private VideoAgent agent = null;
	private RollbackTimer rollbackTimer = null;
	private int c100 = 3;
	private int t100 = 1;

	public VideoPendingReleaseState(Context ctx,VideoAgent agent)
	{
		CallTimer callTimer = agent.getCallTimer();
		this.c100 = callTimer.getC100();
		this.t100 = callTimer.getT100();

		rollbackTimer = new RollbackTimer(ctx,t100,this);

		this.agent = agent;
		Log.d(GroupCallAgent.Log_Key, "currentState is VideoPendingReleaseState");
	}

	public int doUserAction(int btnAction)
	{
		int result = CauseCont.OperateCheck_Suc;
		return result;
	}
	@Override
	public void receiveRtcpMsg(RtcpMsg msg) {
		// TODO Auto-generated method stub
		Log.d(GroupCallAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			return;
		}

		String title = msg.getTitle();
		if(title.equals(RtcpMsg.Title_MCV1_IDLE)
				|| title.equals(RtcpMsg.Title_MCV1_MEDIA_NOTE))
		{
			//			gotoNoPermissionStatus();
			agent.goToState(CallConst.VideoState_NoPermission);
		}
		else if(title.equals(RtcpMsg.Title_MCV1_REVOKED))
		{
			int cause = msg.getCause();
			Log.d(GroupCallAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			//			 agent.notifyRevokeCause(cause);

			//			agent.getFloorMediaOperate().setRtpEnable(LTEMedia.RTP_SENDRECV_DISABLE);
			//			int result =  agent.releaseFloor();
			//			if(result == CallConst.Operate_Suc)
			{
			}
		}
		else if(title.equals(RtcpMsg.Title_MCV1_GRANTED))
		{
		}
	}
	public void updateTakenUser(String callType,AddrInfo gInfo,AddrInfo user)
	{}
	public void destory()
	{
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}

	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(GroupCallAgent.Log_Key, "" + CallConst.stateTag(getState()) + " onTimeout");
		if(c100 > 0)
		{
			if(agent != null)
			{
				int result = agent.transRelease();
				if(result == CallConst.Operate_Suc)
				{}
				if(rollbackTimer != null)
				{
					rollbackTimer.restartTimer(t100);
				}
				c100 --;
			}
		}
		else if(agent != null)
		{
			agent.goToState(CallConst.VideoState_NoPermission);
		}
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return CallConst.VideoState_PendingRelease;
	}
}
